<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Symbol match</title>
</head>

<body>
  <script>
    const myMatcher = {
      [Symbol.match](string) {
        return string.split(' ')
      }
    }
    const inputString = 'Hello World'
    const result = inputString.match(myMatcher)
    RegExp.prototype[Symbol.match] = function (string) {
      console.log(string.includes(this.toString().replace(/\//g, '')))
      return string.split(this)
    }
    console.log(inputString.match(/Hello/))
    class FooMatcher {
      static [Symbol.match](target) {
        return target.includes('foo')
      }
    }
    console.log('foobar'.match(FooMatcher))

    class StringMatcher {
      constructor(str) {
        this.str = str
      }
      [Symbol.match](string) {
        return string.includes(this.str)
      }
    }
    console.log('foobar'.match(new StringMatcher('foo')))
    console.log("foobar".match(new StringMatcher("hello")))
  </script>
</body>

</html>